package com.wchen.gdoc.generate.resource.analysis;

import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.FieldDoc;
import com.sun.javadoc.Type;
import com.wchen.gdoc.generate.config.ApplicationConfigProvide;
import com.wchen.gdoc.generate.resource.structure.FieldStructure;

import java.util.*;


/**
 * 对象类型解析器
 *
 * @author  wchen
 * @date    2019/11/7
 */
@Deprecated
public class CustomTypeDocAnalysis extends ObjectTypeDocAnalysis {

    private FieldTypeFilter fieldTypeFilter = new FieldTypeFilter();

    @Override
    public Boolean isSupport(Type docType) {
//        Map<String, List<String>> exclusiveFieldMap = ApplicationConfigProvide.getApplicationConfig().getTargetProjectConfig().getExclusiveFieldMap();
//        String docTypeName = docType.qualifiedTypeName();
//        return exclusiveFieldMap.containsKey(docTypeName);
        return false;
    }

    @Override
    public FieldStructure analysisParam(Type docType, Boolean isRequestParam) {
        if(ENABLE_DEBUG) {
            LOGGER.info(String.format("execute ObjectTypeDocAnalysis, docType=%s", docType.qualifiedTypeName()));
        }
        Set<String> configFields = matchDealFields(docType.qualifiedTypeName());
        FieldStructure objectTypeStructure = new FieldStructure();
        List<FieldStructure> fieldStructureList = new ArrayList<>();
        for (FieldDoc fieldDoc : docType.asClassDoc().fields(false)) {
            if(fieldTypeFilter.isFieldSkip(fieldDoc, isRequestParam)){
                continue;
            }
            //忽略自定义字段
            if(configFields.contains(fieldDoc.name())) {
                continue;
            }

            FieldStructure fieldStructure = executeAnalysis(fieldDoc.type(), isRequestParam);
            if (Objects.isNull(fieldStructure)){
                continue;
            }
            fieldStructure.setFieldCommentText(fieldDoc.commentText());
            fieldStructure.setType(fieldDoc.type().qualifiedTypeName());
            fieldStructure.setFieldName(fieldDoc.name());
            fieldStructure.setFieldAnnotationList(analysisAnnotationDoc(fieldDoc.annotations()));
            fieldStructureList.add(fieldStructure);
        }
        ClassDoc classDoc = docType.asClassDoc();
        objectTypeStructure.setFieldAnnotationList(analysisAnnotationDoc(classDoc.annotations()));
        objectTypeStructure.setFieldCommentText(classDoc.commentText());
        objectTypeStructure.setCommentText(classDoc.commentText());
        objectTypeStructure.setSimpleName(classDoc.simpleTypeName());
        objectTypeStructure.setType(classDoc.qualifiedTypeName());
        objectTypeStructure.setFieldList(fieldStructureList);
        return objectTypeStructure;
    }

    /**
     *
     * @param classType
     * @return
     */
    private HashSet<String> matchDealFields(String classType) {
        Map<String, List<String>> exclusiveFieldMap = ApplicationConfigProvide.getApplicationConfig().getTargetProjectConfig().getExclusiveFieldMap();
        return  new HashSet<String>(exclusiveFieldMap.get(classType));
    }
}
